package com.qf.shop.utils;

import cn.hutool.core.date.DateUtil;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.schedule.ScheduleClient;
import cn.jpush.api.schedule.ScheduleResult;
import com.qf.shop.model.Results;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 这是极光推送工具类
 */
@Component
public class JPushUtils {

    private static Logger logger = Logger.getLogger(String.valueOf(JPushUtils.class));

    private static String APP_KEY ="882082a29d22497ae4675b9c";
    private static String MASTER_SECRET = "145c4bbc68871c699289d946";

    //创建JPushClient
    private static JPushClient jPushClient = new JPushClient(MASTER_SECRET,APP_KEY);




    //极光推送>>Android
    //Map<String, String> parm是我自己传过来的参数,同学们可以自定义参数

    /**         使用：
     *          Map<String, String> parm = new HashMap<>();
     *         这里的id是,移动端集成极光并登陆后,极光用户的rid,可不要，看.setAudience(Audience.registrationId(parm.get("id")))有没有
     *         parm.put("id", "18071adc03b9843452");
     *         设置提示信息,msg就是信息内容
     *         parm.put("msg","我是靓仔流");
     *         JPushUtils.jpushAndroid(parm);
     * @param parm 参数map
     */
    public static void jpushAndroid(Map<String, String> parm) {


        PushPayload payload0 = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                .setAudience(Audience.all())//你项目中的所有用户
//                .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
                .setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))
                //发送内容
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //这里是指定开发环境,不用设置也没关系
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                .build();

        try {
            PushResult pushResult = jPushClient.sendPush(payload0);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }

    }

    public static void changeJpush(Map<String, String> parm){

        PushPayload payload0 = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                .setAudience(Audience.all())//你项目中的所有用户
//                .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
                .setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))
                //发送内容
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //这里是指定开发环境,不用设置也没关系
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                .build();

        try {
            jPushClient.updateSchedulePush("id",payload0);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    public static void sendJpushAndroid(Map<String, String> parm,Date date) {

        String name = "test2";
        String time = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");

        System.out.println(time);

        PushPayload payload1 = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
//                .setAudience(Audience.all())//你项目中的所有用户
                .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
                .setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))
                //发送内容
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //这里是指定开发环境,不用设置也没关系
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                .build();

        try {
            System.out.println(time);
            ScheduleResult result = jPushClient.createSingleSchedule(name, time, payload1);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }


    }



}